package cn.jbolt.wechat.admin.autoreply;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.enumutil.JBoltEnum;
import cn.jbolt.core.kit.JBoltUserKit;
import cn.jbolt.core.permission.*;
import cn.jbolt.wechat.common.WechatAutoreplyReplyType;
import cn.jbolt.wechat.common.WechatAutoreplyType;
import cn.jbolt.wechat.model.WechatAutoreply;
import cn.jbolt.wechat.permission.WechatPermissionKey;
import cn.jbolt.wechat.service.JBoltWechatAutoReplyService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 微信公众平台自动回复设置管理
 *
 * @ClassName: WechatAutoReplyAdminController
 * @author: JFinal学院-小木 QQ：909854136
 * @date: 2019年6月20日
 */
@UnCheckIfSystemAdmin
@Path("/api/admin/wechatAutoReply")
public class WechatAutoReplyAdminController extends JBoltApiBaseController {
    @Inject
    private JBoltWechatAutoReplyService service;


    @CheckPermission(WechatPermissionKey.WECHAT_AUTOREPLY_SUBSCRIBE)
    public void datas(@Para("mpId") Long mpId, @Para(TYPE_PARAM_NAME) Integer type, @Para(KEYWORDS_PARAM_NAME) String keywords,
                      @Para(PAGE_PARAM_NAME) Integer page, @Para(PAGESIZE_PARAM_NAME) Integer pageSize) {
        Page<WechatAutoreply> data = service.paginateAdminMgrList(mpId, type, keywords, page, pageSize);
        renderJBoltApiSuccessWithData(data);
    }

    @UnCheck
    @Before(WechatAutoReplyMpIdAndTypeValidator.class)
    public void index() {
        Long mpId = getLong(0);
        Integer type = getInt(1);
        Long id = getLong(2);
        //根据TYPE 判断是否有权限
        boolean hasPermission = checkPermission(type);
        if (!hasPermission) {
            renderJBoltApiFail(JBoltMsg.NOPERMISSION);
            return;
        }
        WechatAutoreply wechatAutoreply = service.findById(id);
        if (wechatAutoreply == null) {
            renderJBoltApiFail(JBoltMsg.DATA_NOT_EXIST);
            return;
        }
        if (wechatAutoreply.getMpId().longValue() != mpId.longValue()) {
            renderJBoltApiFail("参数异常:公众平台mpId");
            return;
        }
        if (wechatAutoreply.getType().intValue() != type.intValue()) {
            renderJBoltApiFail("参数异常:公众平台type");
            return;
        }
		renderJBoltApiSuccessWithData(wechatAutoreply);
    }

    @UnCheck
    @Before({WechatAutoReplyMpIdAndTypeValidator.class, Tx.class})
    public void delete() {
        Long mpId = getLong(0);
        Integer type = getInt(1);
        Long id = getLong(2);
        //根据TYPE 判断是否有权限
        boolean hasPermission = checkPermission(type);
        if (!hasPermission) {
			renderJBoltApiFail(JBoltMsg.NOPERMISSION);
            return;
        }
        renderJBoltApiRet(service.deleteAutoreply(mpId, type, id));
    }

    @UnCheck
    @Before(WechatAutoReplyMpIdAndTypeValidator.class)
    public void toggleEnable() {
        Long mpId = getLong(0);
        Integer type = getInt(1);
        Long id = getLong(2);
        //根据TYPE 判断是否有权限
        boolean hasPermission = checkPermission(type);
        if (!hasPermission) {
			renderJBoltApiFail(JBoltMsg.NOPERMISSION);
            return;
        }
        renderJBoltApiRet(service.toggleEnable(mpId, type, id));
    }
	@UnCheck
	public void types() {
		renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(WechatAutoreplyType.class));
	}

    @UnCheck
    public void replyTypes() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(WechatAutoreplyReplyType.class));
    }

    private boolean checkPermission(Integer type) {
        boolean hasPermission = false;
        Long userId = JBoltUserKit.getUserId();
        WechatAutoreplyType wechatAutoreplyType = JBoltEnum.getEnumObjectByValue(WechatAutoreplyType.class, type);
        if (wechatAutoreplyType != null) {
            switch (wechatAutoreplyType) {
                case SUBSCRIBE:
                    hasPermission = JBoltUserAuthKit.hasPermission(userId, true, WechatPermissionKey.WECHAT_AUTOREPLY_SUBSCRIBE);
                    break;
                case KEYWORDS:
                    hasPermission = JBoltUserAuthKit.hasPermission(userId, true, WechatPermissionKey.WECHAT_AUTOREPLY_KEYWORDS);
                    break;
                case DEFAULT:
                    hasPermission = JBoltUserAuthKit.hasPermission(userId, true, WechatPermissionKey.WECHAT_AUTOREPLY_DEFAULT);
                    break;
            }
        }
        return hasPermission;
    }

    @UnCheck
    @Before(WechatAutoReplyMpIdAndTypeValidator.class)
    public void save(@Para("wechatAutoreply") WechatAutoreply autoreply) {
        Long mpId = getLong(0);
        Integer type = getInt(1);
        //根据TYPE 判断是否有权限
        boolean hasPermission = checkPermission(type);
        if (!hasPermission) {
			renderJBoltApiFail(JBoltMsg.NOPERMISSION);
            return;
        }
		renderJBoltApiRet(service.save(mpId, type, autoreply));
    }

    @UnCheck
    @Before(WechatAutoReplyMpIdAndTypeValidator.class)
    public void update(@Para("wechatAutoreply") WechatAutoreply autoreply) {
        Long mpId = getLong(0);
        Integer type = getInt(1);
        //根据TYPE 判断是否有权限
        boolean hasPermission = checkPermission(type);
        if (!hasPermission) {
			renderJBoltApiFail(JBoltMsg.NOPERMISSION);
            return;
        }
		renderJBoltApiRet(service.update(mpId, type, autoreply));
    }

}
